package voloshyn.paliy.ui;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import voloshyn.paliy.arduino.R;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryActivity extends Activity {
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.gallery);
         Gallery gallery=(Gallery) findViewById(R.id.gallery);
         gallery.setAdapter(new GalleryAdapter());    
    }
    
    private class GalleryAdapter extends BaseAdapter
    {	
	private List<Drawable> drawables=new LinkedList<Drawable>();
	public GalleryAdapter() {
	  File dir=getFilesDir();
	  for(final File fileImage : dir.listFiles())
	  {
	     String path=fileImage.getAbsolutePath();
	     if(path.contains(".jpg")||path.contains(".png"))
	     {
		 drawables.add(Drawable.createFromPath(path));
	     }
	  }
	}
	
	@Override
	public int getCount() {
	    return drawables.size();
	}

	@Override
	public Object getItem(int arg0) {
	    return drawables.get(arg0);
	}

	@Override
	public long getItemId(int position) {
	      return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    ImageView imageView=new ImageView(getApplicationContext());
	    imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    imageView.setImageDrawable(drawables.get(position));
	    return imageView;
	}
    }
}
